package cn.edu.sdjzu.xg.bysj.security;


import cn.edu.sdjzu.xg.bysj.domain.User;
import cn.edu.sdjzu.xg.bysj.exception.BysjException;
import cn.edu.sdjzu.xg.bysj.util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;

public final class UserService {
    private static final UserService userService = new UserService();
    private final UserDao userDao = UserDao.getInstance();

    public UserService() {
    }

    public static UserService getInstance() {
        return UserService.userService;
    }

//    public Collection<User> getUsers() throws SQLException {
//        return userDao.findAll();
//	}
//
	public User find(Integer id) throws SQLException, BysjException {
        Connection connection = JdbcHelper.getConn();

        User user = userDao.find(connection, id);
        JdbcHelper.close(connection);
        return user;

	}
//
//	public boolean updateUser(User user)throws SQLException {
//		userDao.delete(user);
//		return userDao.add(user);
//	}
//
//	public boolean addUser(User user)throws SQLException {
//		return userDao.add(user);
//	}
//
//    public boolean deleteUser(Integer id) throws SQLException {
//        User user = this.getUser(id);
//        return this.deleteUser(user);
//    }
//
//    public boolean deleteUser(User user) throws SQLException {
//        return userDao.delete(user);
//    }

    public Actor login(User userToLogin) throws SQLException, BysjException {
        Connection connection = JdbcHelper.getConn();
        Actor actor = userDao.login(connection, userToLogin);
        JdbcHelper.close(connection);
        return actor;
    }

    public Actor login(String username, String password) throws SQLException, BysjException {
        Connection connection = JdbcHelper.getConn();
        Actor actor = userDao.login(connection, username, password);
        JdbcHelper.close(connection);
        return actor;
    }

    public static void main(String[] args) throws BysjException, SQLException {
//        Actor actor = UserService.getInstance().login("u1","u1");
//        System.out.println(actor);
        Actor actor2 = UserService.getInstance().login("su1","su1");
        System.out.println(actor2);
    }
}
